
$(document).ready(function() {
    var validator = $("#registration").validate({
    		rules: { 
            firstname: "required", 
            lastname: "required", 
            username: { 
                required: true, 
                minlength: 2, 
                remote: "/ajax/validate_username/",
            }, 
            email: { 
                required: true, 
                email: true,
            },
            phonenumber: {
                minlength: 10,
            }, 
            password: {
            	 required: true,
                minlength: 5,
            }, 
            passwordconfirmation: {
            	 required: true,
                minlength: 5, 
                equalTo: $("input[name='password']")[0],
            },
            room: {
            	required: true,
            },
            
            checkissuer: "required",
            checknumber: "required",
            bank: "required",
            
            macaddress: {
            	required: true,
            	remote: "/ajax/validate_macaddress/",
            },
            hostname: {
            	required: true,
            	remote: "/ajax/validate_hostname/",
            },
        }, 
        messages: {
        		firstname: "Enter your firstname",
            lastname: "Enter your lastname", 
            username: { 
                required: "Enter a username", 
                minlength: jQuery.format("Enter at least {0} characters"), 
                remote: jQuery.format("{0} is already in use"),
            },
            email: "Enter your email",
            hostname: { 
                required: "Enter a hostname", 
                minlength: jQuery.format("Enter at least {0} characters"), 
                remote: jQuery.format("{0} is already in use or has invalid characters"),
            },
            password: { 
                required: "Provide a password", 
                rangelength: jQuery.format("Enter at least {0} characters") 
            }, 
            passwordconfirmation: { 
                required: "Repeat your password", 
                minlength: jQuery.format("Enter at least {0} characters"), 
                equalTo: "Enter the same password as above" 
            }, 
            room: "Select your room",
            checknumber: "Enter your check number",
            bank: "Please select a bank",
            macaddress: {
            	required: "Provide the MAC address of your computer",
            	remote: jQuery.format("Incorrect value"),
            }
        },
        errorPlacement: function(error, element) {
        		error.appendTo( element.parent().next() ); 
        }, 
        errorContainer: "authform_errors",
        success: function(label) { 
            // set   as text for IE
            label.html(" ").addClass("checked"); 
        },
    });

  // propose username by combining first- and lastname 
  $("#registration_user_username").focus(function() { 
    var firstname = $("#registration_user_firstname").val(); 
    var lastname = $("#registration_user_lastname").val(); 
    if(firstname && lastname && !this.value) { 
      this.value = firstname + "." + lastname; 
    } 
  }); 

  $("#registration_check_checkissuer").focus(function() { 
    var firstname = $("#registration_user_firstname").val(); 
    var lastname = $("#registration_user_lastname").val(); 
    if(firstname && lastname && !this.value) { 
      this.value = firstname + " " + lastname; 
    } 
  });

});

